﻿using Digitalmes.Domain.Entities.Mes.Processes;

namespace Digitalmes.Application.Mes.Processes.Routes.Services.Impl;

/// <summary>
/// 工艺路线应用服务。
/// </summary>
internal sealed class RouteAppicationService(IRepository<ProcRoute> repo, IMemoryCache cache) : IRouteAppicationService
{
    /// <summary>
    /// 工艺路线缓存 Key。
    /// </summary>
    /// <param name="RouteCode">工艺路线编号</param>
    record RouteCacheKey(string RouteCode);

    public async Task<RouteStructDto?> GetRouteAsync(string routeCode)
    {
        return await cache.GetOrCreateAsync(new RouteCacheKey(routeCode), async _ =>
        {
            return await GetRouteWithoutCacheAsync(routeCode);
        });
    }

    private async Task<RouteStructDto?> GetRouteWithoutCacheAsync(string routeCode)
    {
        var route = await repo.FirstOrDefaultAsync(s => s.RouteCode == routeCode);
        return new();
    }

    /// <summary>
    /// 重置工艺路线。
    /// </summary>
    /// <param name="routeCode">工艺路线编号</param>
    public void ResetRoute(string routeCode)
    {
        cache.Remove(new RouteCacheKey(routeCode));
    }
}
